home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / mthr25 / mtcrtlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-29  |  5.8 KB  |  338 lines

  1. /* MicroThread V2.5
  2.  
  3.   A minimal Borland/Turbo C multithreading library for DOS program.
  4.   Written and placed into the public domain by I H Ting. Please read
  5.   the files README.TXT and MTHREAD.TXT for more information.
  6.  
  7.   Comments and suggestions to :
  8.  
  9.       Internet Email : i.h.ting@wlv.ac.uk
  10.  
  11.       Compuserve     : 100023,3363
  12.  
  13.       Otherwise      : I H Ting
  14.                        University of Wolverhampton
  15.                        School of Computing & I. T.
  16.                        Wulfruna Street
  17.                        Wolverhampton WV1 1SB
  18.                        U.K.
  19. */
  20.  
  21. /* MTCRTLIB.C - Semaphored replacements for some of the C runtime
  22.    library functions to prevent re-entrancy problem. This is obviously
  23.    not a complete list, as I have only done the ones that I needed.
  24.    However, it is straight-forward to add your own functions.
  25. */
  26.  
  27.  
  28. #define MTCRTLIB_C
  29. #include "stdio.h"
  30. #include "stdarg.h"
  31. #include "stdlib.h"
  32. #include "conio.h"
  33. #include "mthread.h"
  34.  
  35. extern Semaphore semaCRunTimeLib;
  36.  
  37. int MTfprintf (FILE *stream, const char *format, ...)
  38. {
  39.   va_list argptr;
  40.   int cnt;
  41.   char buffer[512];
  42.  
  43.   MTWait(semaCRunTimeLib);
  44.   va_start(argptr, format);
  45.   cnt = vsprintf(buffer, format, argptr);
  46.   va_end(argptr);
  47.   fprintf(stream,buffer);
  48.   MTSignal(semaCRunTimeLib);
  49.   return(cnt);
  50. }
  51.  
  52. int MTprintf (const char *format, ...)
  53. {
  54.   va_list argptr;
  55.   int cnt;
  56.   char buffer[512];
  57.  
  58.   MTWait(semaCRunTimeLib);
  59.   va_start(argptr, format);
  60.   cnt = vsprintf(buffer, format, argptr);
  61.   va_end(argptr);
  62.   puts(buffer);
  63.   MTSignal(semaCRunTimeLib);
  64.   return(cnt);
  65. }
  66.  
  67.  
  68. int MTfputs(const char *s, FILE *stream)
  69. {
  70.   int retval;
  71.  
  72.   MTWait(semaCRunTimeLib);
  73.   retval = fputs(s,stream);
  74.   MTSignal(semaCRunTimeLib);
  75.   return(retval);
  76. }
  77.  
  78.  
  79. int MTfputc(int c, FILE *stream)
  80. {
  81.   int retval;
  82.  
  83.   MTWait(semaCRunTimeLib);
  84.   retval = fputc(c,stream);
  85.   MTSignal(semaCRunTimeLib);
  86.   return(retval);
  87. }
  88.  
  89.  
  90. FILE *MTfopen(const char *filename, const char *mode)
  91. {
  92.   FILE *retval;
  93.  
  94.   MTWait(semaCRunTimeLib);
  95.   retval = fopen(filename, mode);
  96.   MTSignal(semaCRunTimeLib);
  97.   return(retval);
  98. }
  99.  
  100.  
  101. int MTfclose(FILE *stream)
  102. {
  103.   int retval;
  104.  
  105.   MTWait(semaCRunTimeLib);
  106.   retval = fclose(stream);
  107.   MTSignal(semaCRunTimeLib);
  108.   return(retval);
  109. }
  110.  
  111.  
  112. int MTfeof(FILE *stream)
  113. {
  114.   int retval;
  115.  
  116.   MTWait(semaCRunTimeLib);
  117.   retval = feof(stream);
  118.   MTSignal(semaCRunTimeLib);
  119.   return(retval);
  120. }
  121.  
  122.  
  123.  
  124. int MTsprintf (char *buffer, const char *format, ...)
  125. {
  126.   va_list argptr;
  127.   int cnt;
  128.  
  129.   MTWait(semaCRunTimeLib);
  130.   va_start(argptr, format);
  131.   cnt = vsprintf(buffer, format, argptr);
  132.   va_end(argptr);
  133.   MTSignal(semaCRunTimeLib);
  134.   return(cnt);
  135. }
  136.  
  137.  
  138. char *MTfgets(char *s, int n, FILE *stream)
  139. {
  140.   char *retval=NULL;
  141.  
  142.   MTWait(semaCRunTimeLib);
  143.   retval = fgets(s, n, stream);
  144.   MTSignal(semaCRunTimeLib);
  145.   return(retval);
  146. }
  147.  
  148.  
  149. int MTfgetc(FILE *stream)
  150. {
  151.   int retval=NULL;
  152.  
  153.   MTWait(semaCRunTimeLib);
  154.   retval = fgetc(stream);
  155.   MTSignal(semaCRunTimeLib);
  156.   return(retval);
  157. }
  158.  
  159.  
  160.  
  161.  
  162. int MTxyputc(int x, int y, int c)
  163. {
  164.   int retval,lastx,lasty;
  165.  
  166.   MTWait(semaCRunTimeLib);
  167.   lastx=wherex();
  168.   lasty=wherey();
  169.   gotoxy(x,y);
  170.   retval = putch(c);
  171.   gotoxy(lastx,lasty);
  172.   MTSignal(semaCRunTimeLib);
  173.   return(retval);
  174. }
  175.  
  176.  
  177. int MTxyputs(int x, int y, const char *s)
  178. {
  179.   int retval,lastx,lasty;
  180.  
  181.   MTWait(semaCRunTimeLib);
  182.   lastx=wherex();
  183.   lasty=wherey();
  184.   gotoxy(x,y);
  185.   retval = cputs(s);
  186.   gotoxy(lastx,lasty);
  187.   MTSignal(semaCRunTimeLib);
  188.   return(retval);
  189. }
  190.  
  191.  
  192.  
  193. int MTxyprintf (int x, int y, const char *format, ...)
  194. {
  195.   va_list argptr;
  196.   int cnt,lastx,lasty;
  197.   static char buffer[512];
  198.  
  199.   MTWait(semaCRunTimeLib);
  200.   va_start(argptr, format);
  201.   cnt = vsprintf(buffer, format, argptr);
  202.   va_end(argptr);
  203.   lastx=wherex();
  204.   lasty=wherey();
  205.   gotoxy(x,y);
  206.   cputs(buffer);
  207.   gotoxy(lastx,lasty);
  208.   MTSignal(semaCRunTimeLib);
  209.   return(cnt);
  210. }
  211.  
  212.  
  213. int MTkbhit(void)
  214. {
  215.   int retval;
  216.  
  217.   MTWait(semaCRunTimeLib);
  218.   retval = kbhit();
  219.   MTSignal(semaCRunTimeLib);
  220.   return(retval);
  221. }
  222.  
  223.  
  224. int MTgetch(void)
  225. {
  226.   int retval=0;
  227.  
  228.   MTWait(semaCRunTimeLib);
  229.   if (kbhit())
  230.     retval =getch();
  231.   MTSignal(semaCRunTimeLib);
  232.   return(retval);
  233. }
  234.  
  235.  
  236.  
  237. int MTrandom(int num)
  238. {
  239.   int retval;
  240.   MTWait(semaCRunTimeLib);
  241.   retval =random(num);
  242.   MTSignal(semaCRunTimeLib);
  243.   return(retval);
  244. }
  245.  
  246. void MTclrscr(void)
  247. {
  248.   MTWait(semaCRunTimeLib);
  249.   clrscr();
  250.   MTSignal(semaCRunTimeLib);
  251. }
  252.  
  253.  
  254. char * MTgets(char *buf)
  255. {
  256.   char *ps=buf,theChar;
  257.  
  258.   while(1){
  259.     MTWait(semaCRunTimeLib);
  260.     theChar=0;
  261.     if (kbhit()){
  262.       theChar=getch();
  263.     }
  264.     MTSignal(semaCRunTimeLib);
  265.     if(theChar=='\b'){
  266.       if(ps!=buf){
  267.         ps--;
  268.         MTfputs("\b \b",stdout);
  269.       }
  270.     }
  271.     else if (theChar=='\n'||theChar=='\r'){
  272.       *ps='\0';
  273.      return buf;
  274.     }
  275.     else if (theChar){
  276.       MTfputc(theChar,stdout);
  277.       *ps=theChar;
  278.       ps++;
  279.     }
  280.   }
  281. }
  282.  
  283.  
  284.  
  285. void *MTmalloc(size_t size)
  286. {
  287.   void *retval;
  288.  
  289.   MTWait(semaCRunTimeLib);
  290.   retval = malloc(size);
  291.   MTSignal(semaCRunTimeLib);
  292.   return(retval);
  293. }
  294.  
  295.  
  296. void MTfree(void *block)
  297. {
  298.   MTWait(semaCRunTimeLib);
  299.   free(block);
  300.   MTSignal(semaCRunTimeLib);
  301. }
  302.  
  303.  
  304.  
  305.  
  306. char *MTstrcpy(char *dest,char *source)
  307. {
  308.   char *pSource=source, *pDest=dest;
  309.  
  310.   while(*pSource){
  311.     *pDest++ = *pSource++;
  312.   }
  313.   *pDest='\0';
  314.   return(dest);
  315. }
  316.  
  317.  
  318. size_t MTfread(void *ptr, size_t size, size_t n, FILE *stream)
  319. {
  320.   size_t retval=0;
  321.  
  322.   MTWait(semaCRunTimeLib);
  323.   retval = fread(ptr, size, n, stream);
  324.   MTSignal(semaCRunTimeLib);
  325.   return(retval);
  326. }
  327.  
  328.  
  329. size_t MTfwrite(const void *ptr, size_t size, size_t n, FILE*stream)
  330. {
  331.   size_t retval=0;
  332.  
  333.   MTWait(semaCRunTimeLib);
  334.   retval = fwrite(ptr, size, n, stream);
  335.   MTSignal(semaCRunTimeLib);
  336.   return(retval);
  337. }
  338.